package com.cotton.utils;

import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 版权：上海投投金融信息服务有限公司
 * 功能：工具类
 * 作者：朱伟峰
 * 邮箱：zhuweifeng24@gmail.com
 * 日期：2015年11月26日
 */
public class Tools
{
	private static final Logger logger = LoggerFactory.getLogger(Tools.class);
	
	public static Map<String, Object> setResultMap(int status, String reason)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("status", status);
		result.put("reason", reason);
		return result;
	}
	
	public static String genNull(String str)
	{
		return str == null ? "" : str;
	}

	public static boolean isEmpty(String str)
	{
		return (str == null) || (str.equals(""));
	}

	public static boolean isTrimEmpty(String str)
	{
		return (str == null) || (str.equals("")) || (isEmpty(str.trim()));
	}

	public static int genInt(String sValue)
	{
		int iValue;
		try
		{
			iValue = Integer.parseInt(sValue);
		}
		catch (Exception ex)
		{
			iValue = 0;
			logger.error("exception is ....",ex);
		}

		return iValue;
	}

	public static long genLong(String sValue)
	{
		long lValue;
		try
		{
			lValue = Long.parseLong(sValue);
		}
		catch (Exception ex)
		{
			lValue = 0L;
			logger.error("exception is ....",ex);
		}

		return lValue;
	}

	public static short genShort(String sValue)
	{
		short iValue;
		try
		{
			iValue = Short.parseShort(sValue);
		}
		catch (Exception ex)
		{
			iValue = 0;
			logger.error("exception is ....",ex);
		}

		return iValue;
	}

	public static short genByte(String sValue)
	{
		byte bValue;
		try
		{
			bValue = Byte.parseByte(sValue);
		}
		catch (Exception ex)
		{
			bValue = 0;
			logger.error("exception is ....",ex);
		}

		return bValue;
	}

	public static float genFloat(String sValue)
	{
		float fValue;
		try
		{
			fValue = Float.parseFloat(sValue);
		}
		catch (Exception ex)
		{
			fValue = 0.0F;
			logger.error("exception is ....",ex);
		}

		return fValue;
	}

	public static double genDouble(String sValue)
	{
		double fValue;
		try
		{
			fValue = Double.parseDouble(sValue);
		}
		catch (Exception ex)
		{
			fValue = 0.0D;
			logger.error("exception is ....",ex);
		}

		return fValue;
	}

	public static Date genDate(String sValue, String sFormat)
	{
		Date dValue;
		try
		{
			if ((sFormat == null) || (sFormat.trim().equals("")))
			{
				sFormat = "yyyy-MM-dd HH:mm:ss";
			}
			SimpleDateFormat dateFormat = new SimpleDateFormat(sFormat);

			dValue = dateFormat.parse(sValue);
		}
		catch (Exception ex)
		{
			dValue = new Date(0L);
			logger.error("exception is ....",ex);
		}

		return dValue;
	}

	public static Date genDate(String sValue)
	{
		return genDate(sValue, "");
	}

	public static Date genJustDate(String sValue)
	{
		return genDate(sValue, "yyyy-MM-dd");
	}

	public static Date genDate(long lValue, String sFormat)
	{
		if ((sFormat == null) || (sFormat.trim().equals("")))
		{
			sFormat = "yyyy-MM-dd HH:mm:ss";
		}
		return genDate(formatDate(new Date(lValue), sFormat), sFormat);
	}

	public static long genLong(Date date)
	{
		if (date != null) { return date.getTime(); }
		return 0L;
	}

	public static Date genDate(long value)
	{
		if (value != 0L) { return genDate(value, ""); }
		return null;
	}

	public static Date genJustDate(long lValue)
	{
		return genDate(lValue, "yyyy-MM-dd");
	}

	public static double formatData(double fData, String sFormatString)
	{
		try
		{
			DecimalFormat df = new DecimalFormat(sFormatString);

			return Double.parseDouble(df.format(fData));
		}
		catch (NumberFormatException nfe)
		{
			System.out.println("fData========" + fData);
		}
		return 0.0D;
	}

	public static String formatDate(long lDate, String sFormat)
	{
		return formatDate(new Date(lDate), sFormat);
	}

	public static String formatDate(long lDate)
	{
		return formatDate(new Date(lDate), "");
	}

	public static String formatTimes(long lDate, String level)
	{
		String dateStr = "";
		int days = 0;
		int hours = 0;
		int minutes = 0;
		long timesHour = 0L;
		long timesMinute = 0L;
		long currentTime = new Date().getTime();
		if (currentTime >= lDate)
		{
			long times = currentTime - lDate;
			days = (int) (times / 86400000L);
			timesHour = times % 86400000L;
			hours = (int) (timesHour / 3600000L);
			timesMinute = timesHour % 3600000L;
			minutes = (int) (timesMinute / 60000L);
			if ("".equals(level))
			{
				if ((days == 0) && (hours == 0) && (minutes == 0))
				{
					dateStr = "刚刚";
				}
				else if ((days != 0) && (hours == 0) && (minutes == 0))
				{
					dateStr = String.valueOf(days) + "天前";
				}
				else if ((days == 0) && (hours != 0) && (minutes == 0))
				{
					dateStr = String.valueOf(hours) + "小时前";
				}
				else if ((days == 0) && (hours == 0) && (minutes != 0))
				{
					dateStr = String.valueOf(minutes) + "分钟前";
				}
				else if ((days != 0) && (hours == 0) && (minutes != 0))
				{
					dateStr = String.valueOf(days) + "天前";
				}
				else if ((days != 0) && (hours != 0) && (minutes == 0))
				{
					dateStr = String.valueOf(days) + "天"
							+ String.valueOf(hours) + "小时前";
				}
				else if ((days == 0) && (hours != 0) && (minutes != 0))
				{
					dateStr = String.valueOf(hours) + "小时"
							+ String.valueOf(minutes) + "分钟前";
				}
				else
				{
					dateStr = String.valueOf(days) + "天"
							+ String.valueOf(hours) + "小时前";
				}
			}
			else if ("1".equals(level))
				if ((days == 0) && (hours == 0) && (minutes == 0))
				{
					dateStr = "刚刚";
				}
				else if ((days != 0) && (hours == 0) && (minutes == 0))
				{
					dateStr = String.valueOf(days) + "天前";
				}
				else if ((days == 0) && (hours != 0) && (minutes == 0))
				{
					dateStr = String.valueOf(hours) + "小时前";
				}
				else if ((days == 0) && (hours == 0) && (minutes != 0))
				{
					dateStr = String.valueOf(minutes) + "分钟前";
				}
				else if ((days != 0) && (hours == 0) && (minutes != 0))
				{
					dateStr = String.valueOf(days) + "天前";
				}
				else if ((days != 0) && (hours != 0) && (minutes == 0))
				{
					dateStr = String.valueOf(days) + "天前";
				}
				else if ((days == 0) && (hours != 0) && (minutes != 0))
				{
					dateStr = String.valueOf(hours) + "小时前";
				}
				else dateStr = String.valueOf(days) + "天前";
		}
		else
		{
			dateStr = "刚刚";
		}

		return dateStr;
	}

	public String formatTimes(Date date)
	{
		long lDate = date.getTime();
		return formatTimes(lDate, "");
	}

	public static String formatJustDate(long lDate)
	{
		return formatDate(new Date(lDate), "yyyy-MM-dd");
	}

	public static String formatDate(Date date, String sFormat)
	{
		if (date == null)
			return "";
		
		if ((sFormat == null) || (sFormat.trim().equals("")))
		{
			sFormat = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat df = new SimpleDateFormat(sFormat);

		return df.format(date);
	}
	
	public static String ajaxEncode (String str, String encode)
	{
		String encodeStr = "";
		try {
			encodeStr = java.net.URLDecoder.decode(str,encode);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} 
		return encodeStr;
	}
	
    /**
	 * 给指定日期增加天数
	 * 
	 * @param strDate	指定日期 yyyy-MM-dd格式
	 * @param day		增加天数
	 * 
	 * @return			增加天数后的日期 yyyy-MM-dd格式
	 */
	public static String addDate(String strDate, int day)
	{
		if (isEmpty(strDate) || day == 0)
    		return "";
		
		String sFormat = "yyyy-MM-dd";
		
		try
		{
			SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(sFormat);
			Date dateYMD = localSimpleDateFormat.parse(strDate);
			SimpleDateFormat format = new SimpleDateFormat(sFormat);

			// 增加天数
			Calendar ca = Calendar.getInstance();
			ca.setTime(dateYMD);
			ca.add(Calendar.DATE, day);
			dateYMD = ca.getTime();
			
			String backTime = format.format(dateYMD);
			return backTime;
		}
		catch (Exception e)
		{
			logger.error("strDate：" + strDate + " day：" + day);
		}
		return "";
	}
	
	/**
	 * 给指定日期增加天数
	 * 
	 * @param strDate	指定日期 yyyy-MM-dd格式
	 * @param day		增加天数
	 * 
	 * @return			增加天数后的日期 yyyy-MM-dd格式
	 */
	public static Date addDate(Date date, int day)
	{
		try
		{
			// 增加天数
			Calendar ca = Calendar.getInstance();
			ca.setTime(date);
			ca.add(Calendar.DATE, day);
			
			return ca.getTime();
		}
		catch (Exception e)
		{
			logger.error("date：" + date + " day：" + day);
		}
		return null;
	}
	
	/**
	 * 
	 * <p>Description:判断list是否为空</p>
	 * @param list
	 * @return
	 * @author 陈姣姣
	 * @date 2016年4月25日 下午3:17:10
	 */
    public static <T> boolean isEmpty(List<T> list)
    {
        if (list == null || list.size() == 0)
            return true;
        return false;
    }
    
    /**
     * <p>
     * Description:校验参数是否存在空值
     * </p>
     * 
     * @param params 多个参数信息
     * @return 其中一个参数存在空值返回tue，否则false
     * @author 廖明华
     * @date 2015年8月27日 上午1:14:15
     */
    public static boolean existEmpty(String... params)
    {
        for (String str : params)
            if (isEmpty(str))
                return true;

        return false;
    }
    
}
